/**
 * 消息通知 handlers
 * @type {{DeviceReboot: Function, DeviceStatusChange: Function, ConfigModify: Function, ConfigCheck: Function, SendConfig: Function}}
 */
noticeHandlers = {
    //设备重启
    DeviceReboot: function(msg){
        var opts = {
            title: '设备重启通知',
            text: '主控端已收到设备【'+msg.data.dpi_ip+'】重启指令.',
            hide: true,
            delay: 5000,
            type: "info",
            buttons: {
                sticker: false
            },
            /*stack: stack_context*/
        };
        new PNotify(opts);
    },
    //设备状态变更
    DeviceStatusChange:function(msg){
        var _data1 = msg.data;
        var opts;

        switch (_data1.dev_status) {
            case 1:
                opts = {
                    title: '注册通知',
                    text: _data1.dpi_ip + '设备注册.',
                    hide: true,
                    delay: 5000,
                    type: "info",
                    buttons: {
                        sticker: false
                    },
                };
                appendCode(_data1);
                new PNotify(opts);

                break;
            case 2:
                opts = {
                    title: '上线通知',
                    text: _data1.dpi_ip + '设备上线.',
                    hide: true,
                    delay: 5000,
                    type: "info",
                    buttons: {
                        sticker: false
                    },

                };
                new PNotify(opts);
                break;
            case 3:
                opts = {
                    title: '下线通知',
                    text: _data1.dpi_ip + '设备下线.',
                    hide: true,
                    delay: 5000,
                    type: "info",
                    buttons: {
                        sticker: false
                    },

                };
                new PNotify(opts);
                break;
            case 4:
                opts = {
                    title: '重启通知',
                    text: _data1.dpi_ip + '设备重启.',
                    hide: true,
                    delay: 5000,
                    type: "info",
                    buttons: {
                        sticker: false
                    },

                };
                new PNotify(opts);
                break;
        }

    },
    //配置项修改
    ConfigModify:function(msg){
        var hide=true;
        if(msg.data[0]=="error")
        {
            hide=false;
        }
        var opts = {
            title: '配置项修改结果',
            text: msg.data[1],
            hide: hide,
            delay: 5000,   //弹框显示t秒后消失
            type: msg.data[0],
            buttons: {
                sticker: false
            }
        };
        new PNotify(opts);
    },
    //一致性检查
    ConfigCheck:function(msg){
        var opts;
        var hide=true;
        if(msg.data[0]=="error")
        {
            hide=false;
        }
        if(msg.data[0]=='success'){
            var url=ctx+msg.data[2];
             opts = {
                title: '一次性检查结果',
                text: msg.data[1]+'<a href="'+url+'" target="content">'+'刷新'+'</a>',
                hide: true,
                delay: 5000,   //弹框显示t秒后消失
                type: msg.data[0],
                buttons: {
                    sticker: false
                }
            };
        }else{
             opts = {
                title: '一次性检查结果',
                text: msg.data[1],
                hide: hide,
                delay: 5000,   //弹框显示t秒后消失
                type: msg.data[0],
                buttons: {
                    sticker: false
                }
            };
        }

        new PNotify(opts);
    },
    //全量配置项下发
    SendConfig:function(msg){
        var hide=true;
        if(msg.data[0]=="error")
        {
            hide=false;
        }
        var opts = {
                title: '全量配置项下发主控端回复',
                text: msg.data[1],
                hide: hide,
                delay: 5000,   //弹框显示t秒后消失
                type: msg.data[0],
                buttons: {
                    sticker: false
                }
            };
        new PNotify(opts);
    }
}

function connect(targetadd) {

    var target = targetadd;     //服务器地址

    ws = new WebSocket(target);

    ws.onopen = function () {

        //alert('Info: WebSocket connection opened.');
    };

    ws.onmessage = function (event) {

        var str = event.data;
        var _data = "";
        try {
            _data = $.parseJSON(str);
        } catch (e) {
            return;
        }

        noticeHandlers[_data.type].call(this, _data);


    };

    ws.onclose = function (event) {
      window.location= ctx + "/login"
        // alert('Info: WebSocket connection closed, Code: ' + event.code + (event.reason == "" ? "" : ", Reason: " + event.reason));
    };

}

//test
function echo() {
    if (ws != null) {
        var message = {
            "type": "DeviceStatusChange",
            "data": {
                "nodeCode": "NODE1",
                "nodeName": "",
                "location": "",
                "devType": "4",
                "typeNum": "",
                "devName": "yyyyyyyy",
                "id": "",
                "ip": "172.16.2.3",
                "devId": "",
                "dev_status": 1

            }
        };
        ws.send(JSON.stringify(message));
    } else {
        alert('WebSocket connection not established, please connect.');
    }
}

function disconnect() {
    if (ws != null) {
        ws.close();
        ws = null;
    }

}